package com.hyyt.shopjsp.customer.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.wxmg.util.WxBase64;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicCustomer;
import com.hyyt.shopjsp.basic.service.IBasicCustomerService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.customer.pojo.DisCustomer;
import com.hyyt.shopjsp.customer.service.IDisCustomerService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/31 0024下午 16:53.
 */
@Controller
@RequestMapping("/back/disCustomer")
public class DisCustomerController extends BaseController {
	private Logger log = Logger.getLogger(DisCustomerController.class);
	private String basePath = "/back/distribution/customer/";

	@Resource
	private IDisCustomerService disCustomerService;//分销service
	@Resource
	private IBasicCustomerService basicCustomerService;//会员service
	/**
	 * 进入会员分销管理
	 * @param customerId
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoLowePage")
	public String gotoLowePage(String customerId, Map<String, Object> param){
		param.put("level1Id", customerId);
		int totalRecordCount1=disCustomerService.countDis(param);//一级
		param.clear();
		param.put("level2Id", customerId);
		int totalRecordCount2=disCustomerService.countDis(param);//二级
		param.clear();
		param.put("level3Id", customerId);
		int totalRecordCount3=disCustomerService.countDis(param);//二级
		param.clear();
        param.put("customerId", customerId);
        param.put("level1", totalRecordCount1);
        param.put("level2", totalRecordCount2);
        param.put("level3", totalRecordCount3);
		return basePath+"customerLoweList";
	}

	/**
	 * 查询所有会员分销
	 * @param customerId
	 * @param loginName
	 * @param wxName
	 * @param levelId
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="listLoweCustomer",method=RequestMethod.POST)
	@ResponseBody
	public void listLoweCustomer(String customerId,String loginName,String wxName,String levelId,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(loginName)){//是否检索类型名称
			param.put("loginName", loginName.trim());
		}
		if(Utils.objectIsNotEmpty(wxName)){//是否检索类型名称
			param.put("wxName", wxName.trim());
		}
		if(("1").equals(levelId)){//是否检索类型名称
			param.put("level2Id", customerId);
		}else if(("2").equals(levelId)){
			param.put("level3Id", customerId);
		}else{
			param.put("level1Id", customerId);
		}
		param.put("sortColumns", "a.registerDate desc");
		int totalRecordCount =disCustomerService.countDisCustomer(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<DisCustomer> customerList =disCustomerService.queryDisCustomer(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		if(customerList!=null&&customerList.size()>0){
			for (DisCustomer disCustomer : customerList) {
				if(Utils.objectIsNotEmpty(disCustomer.getWxName())){
					String name= WxBase64.decode(disCustomer.getWxName());
					disCustomer.setWxName(name);
				}else{
					disCustomer.setWxName(disCustomer.getLoginName());
				}
				param.put("customerId", customerId);
				List<BasicCustomer> moneyList=basicCustomerService.querySum(param);
				if(moneyList!=null&&moneyList.size()>0 && moneyList.isEmpty()){
					for (BasicCustomer basicCustomer : moneyList) {
						disCustomer.setMoney(basicCustomer.getAmount());
					}
				}else{
					disCustomer.setMoney(new BigDecimal("0"));
				}
			}
		}
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", customerList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 查看分销返利
	 * @param customerId
	 * @param levelId
	 * @param loginName
	 * @param orderNo
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="getReturnAmount",method=RequestMethod.POST)
	@ResponseBody
	public void getReturnAmount(String customerId, String levelId,String loginName,String orderNo,Page page,HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		if(Utils.stringIsNotEmpty(customerId)){
			param.put("customerId", customerId);
		}
		if(Utils.stringIsNotEmpty(loginName)){
			param.put("loginName", loginName);
		}
		if(Utils.stringIsNotEmpty(orderNo)){
			param.put("ordersNo", orderNo);
		}
		int totalRecordCount =basicCustomerService.countReturnAmount(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<BasicCustomer> customerList =basicCustomerService.queryReturnAmount(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", customerList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}
}